/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.BorderFactory;

import org.apache.log4j.Logger;
import javax.swing.JCheckBox;

public class JDialogImageOutputSize extends JDialog {

    private static Logger logger = Logger.getLogger(JDialogImageOutputSize.class);
	
	private static final long serialVersionUID = 1L;

	private int imgWidth, imgHeight;
	private boolean okPressed =false;
	
	private JPanel jContentPane = null;
	private JPanel jPanelInfos = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;

	private JPanel jPanelLabel = null;
	private JLabel jLabel = null;
	private JPanel jPanelInput = null;
	private JLabel jLabelWidth = null;
	private JTextField jTextFieldWidth = null;
	private JLabel jLabelHeight = null;
	private JTextField jTextFieldHeight = null;

	private JCheckBox jCheckBoxRemoveBlankEdges = null;

	private JPanel jPanelRemoveEdges = null;

	/**
	 * @param owner
	 */
	public JDialogImageOutputSize(Frame owner, int width, int height) {
		super(owner);
		initialize();
		
		imgWidth =width;
		imgHeight =height;
		
		getJTextFieldWidth().setText( String.valueOf( width ) );
		getJTextFieldHeight().setText( String.valueOf( height ) );		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setResizable( false );
		this.setTitle("Image output size");
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelInfos(), BorderLayout.CENTER);
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelInfos	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInfos() {
		if (jPanelInfos == null) {
			jPanelInfos = new JPanel();
			jPanelInfos.setLayout(new BorderLayout());
			jPanelInfos.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
			jPanelInfos.add(getJPanelLabel(), BorderLayout.NORTH);
			jPanelInfos.add(getJPanelInput(), BorderLayout.CENTER);
			jPanelInfos.add(getJPanelRemoveEdges(), BorderLayout.SOUTH);
		}
		return jPanelInfos;
	}

	/**
	 * This method initializes jPanelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					okPressed =true;
					try {
						imgWidth =Integer.parseInt( getJTextFieldWidth().getText() );
						imgHeight =Integer.parseInt( getJTextFieldHeight().getText() );
					} catch( Exception e2 ) {
						logger.warn( e2 );
					}
					setVisible( false );
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jPanelLabel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelLabel() {
		if (jPanelLabel == null) {
			jLabel = new JLabel();
			jLabel.setText("Please enter the dimensions in pixels");
			jPanelLabel = new JPanel();
			jPanelLabel.setLayout(new GridBagLayout());
			jPanelLabel.add(jLabel, new GridBagConstraints());
		}
		return jPanelLabel;
	}

	/**
	 * This method initializes jPanelInput	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInput() {
		if (jPanelInput == null) {
			jLabelHeight = new JLabel();
			jLabelHeight.setText("Height");
			jLabelWidth = new JLabel();
			jLabelWidth.setText("Width");
			jPanelInput = new JPanel();
			jPanelInput.setLayout(new GridLayout(2,2,5,5));
			jPanelInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
			jPanelInput.add(jLabelWidth, null);
			jPanelInput.add(getJTextFieldWidth(), null);
			jPanelInput.add(jLabelHeight, null);
			jPanelInput.add(getJTextFieldHeight(), null);
		}
		return jPanelInput;
	}

	/**
	 * This method initializes jTextFieldWidth	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldWidth() {
		if (jTextFieldWidth == null) {
			jTextFieldWidth = new JTextField();
		}
		return jTextFieldWidth;
	}

	/**
	 * This method initializes jTextFieldHeight	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldHeight() {
		if (jTextFieldHeight == null) {
			jTextFieldHeight = new JTextField();
		}
		return jTextFieldHeight;
	}

	public int getImgHeight() {
		return imgHeight;
	}

	public int getImgWidth() {
		return imgWidth;
	}

	public boolean isOkPressed() {
		return okPressed;
	}

	/**
	 * This method initializes jCheckBoxRemoveBlankEdges	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	public JCheckBox getJCheckBoxRemoveBlankEdges() {
		if (jCheckBoxRemoveBlankEdges == null) {
			jCheckBoxRemoveBlankEdges = new JCheckBox();
			jCheckBoxRemoveBlankEdges.setText("Remove blank edges");
			jCheckBoxRemoveBlankEdges.setSelected( true );
		}
		return jCheckBoxRemoveBlankEdges;
	}

	/**
	 * This method initializes jPanelRemoveEdges	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelRemoveEdges() {
		if (jPanelRemoveEdges == null) {
			jPanelRemoveEdges = new JPanel();
			jPanelRemoveEdges.setLayout(new GridBagLayout());
			jPanelRemoveEdges.add(getJCheckBoxRemoveBlankEdges(), new GridBagConstraints());
		}
		return jPanelRemoveEdges;
	}		
	
	public boolean isRemoveBlankEdges() {
		return jCheckBoxRemoveBlankEdges.isSelected();
	}

}
